<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%cms_special_article}}".
 *
 * @property int $id
 * @property int $special_id
 * @property int $special_channel_id
 * @property int $article_id
 * @property string|null $title
 * @property string|null $thumb
 * @property string|null $description
 * @property int $position_id 推荐位置ID
 * @property int $recommend_at 推荐时间
 * @property int $listorder
 * @property int $created_at
 * @property int $updated_at
 */
class CmsSpecialArticle extends \common\db\ActiveRecord {

    /**
     * 文章格式
     */
    const FORMAT_DEFAULT = 'default';
    const FORMAT_GALLERY = 'gallery';
    const FORMAT_VIDEO = 'video';
    const FORMAT_AUDIO = 'audio';
    const FORMAT_CALENDAR = 'calendar';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_special_article}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['special_id', 'special_channel_id', 'article_id', 'position_id', 'recommend_at', 'listorder', 'created_at', 'updated_at'], 'integer'],
            [['title'], 'string', 'max' => 100],
            [['thumb', 'description'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'special_id' => 'Special ID',
            'special_channel_id' => 'Special Channel ID',
            'article_id' => 'Article ID',
            'title' => 'Title',
            'thumb' => 'Thumb',
            'description' => 'Description',
            'position_id' => '推荐位置ID',
            'recommend_at' => '推荐结束时间',
            'listorder' => 'Listorder',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getSpecial() {
        return $this->hasOne(CmsSpecial::class, ['id' => 'special_id']);
    }

    public function getSpecialChannel() {
        return $this->hasOne(CmsSpecialChannel::class, ['id' => 'special_channel_id']);
    }

    public function getArticle() {
        return $this->hasOne(CmsArticle::class, ['id' => 'article_id']);
    }

    public function getCalendar() {
        return $this->hasOne(CmsArticleCalendar::class, ['article_id' => 'article_id']);
    }

    public function getShowTitle() {
        if ($this->title) {
            return $this->title;
        }
        if ($this->article) {
            return $this->article->title;
        }
        return '';
    }

    public function getShowThumb() {
        $thumb = $this->thumb;
        if (!$thumb && $this->article) {
            $thumb = $this->article->thumb;
        }
        return Url::attach($thumb);
    }

    public function getDuration() {
        if ($this->article) {
            return $this->article->durationTime;
        }
        return '';
    }

    public function getShowDescription() {
        if ($this->description) {
            return $this->description;
        }
        if ($this->article) {
            if ($this->article->format == CmsArticle::FORMAT_AUDIO) {
                return $this->article->audioMark;
            }
            return $this->article->description;
        }
        return '';
    }

    public function getPublishTime() {
        if ($this->article) {
            return $this->article->publishTime;
        }
        return $this->created_at ? date('Y-m-d', $this->created_at) : date('Y-m-d');
    }

    /**
     * 获取专题文章围栏地址
     * @return \common\logics\SiteMapLogic|null
     */
    public function getWeblink() {
        $article = $this->article;
        $special = $this->special;
        if (!$special || !$article) {
            return null;
        }
        $names = [];
        if ($special->template) {
            $names[] = $special->template;
        }
        $char = '.';
        if ($article->format == static::FORMAT_DEFAULT) {
            $char = '/';
        }
        $articlePath = explode($char, $article->mapPath);
        $path = implode($char, array_merge($names, $articlePath));
        $data = [
            'id' => $this->article_id,
            '_title' => $this->showTitle,
        ];
        return Url::weblink($path, $data);
    }

}
